package com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.group;

import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.webmanager.business.maintain_performance.constant.ConditionLogic;
import com.tbit.uqbike.webmanager.business.maintain_performance.constant.PerformanceBizType;
import com.tbit.uqbike.webmanager.business.maintain_performance.constant.PerformanceRuleType;
import com.tbit.uqbike.webmanager.business.maintain_performance.constant.RuleLogic;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.PerformanceRule;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.PerformanceRuleCondition;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.PerformanceRuleGroup;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.BizRuleGroup;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.rule.AbnormalSolvedRule;
import lombok.Data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 异常车辆处理规则组
 *
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2025/2/21
 */
@Data
public class AbnormalSolvedGroup implements BizRuleGroup {
    /**
     * 是否启用
     */
    private Boolean enabled;
    /**
     * 消除异常绩效规则
     */
    private List<AbnormalSolvedRule> subRules;

    @Override
    public PerformanceRuleGroup toRaw(PerformanceBizType bizType, Integer accountId) {
        PerformanceRuleGroup group = new PerformanceRuleGroup();
        group.setAccountId(accountId);
        group.setBizType(bizType);
        group.setRuleType(PerformanceRuleType.MACHINE_ABNORMAL);
        group.setEnabled(enabled);
        group.setRules(new ArrayList<>());

        if (subRules == null) {
            return group;
        }
        for (AbnormalSolvedRule rule : subRules) {
            PerformanceRule performanceRule = new PerformanceRule();
            if (rule.getPerformance() == null) {
                throw new BaseException("异常车辆处理绩效-绩效值不能为空");
            }
            performanceRule.setPerformance(rule.getPerformance());
            performanceRule.setConditionLogic(ConditionLogic.OR);
            performanceRule.setConditions(new ArrayList<>());
            if (rule.getAbnormalTypes() != null) {
                for (Integer abnormalType : rule.getAbnormalTypes()) {
                    PerformanceRuleCondition condition = new PerformanceRuleCondition();
                    condition.setKey("abnormalType");
                    condition.setLogic(RuleLogic.EQUAL);
                    condition.setValue(abnormalType.toString());
                    performanceRule.getConditions().add(condition);
                }
            }
            group.getRules().add(performanceRule);
        }
        return group;
    }

    @Override
    public void initFromRaw(PerformanceRuleGroup group) {
        setEnabled(group.getEnabled());
        subRules = new ArrayList<>();
        for (PerformanceRule rule : group.getRules()) {
            AbnormalSolvedRule subRule = new AbnormalSolvedRule();
            subRule.setPerformance(rule.getPerformance());
            List<Integer> abnormalTypes = new ArrayList<>();
            for (PerformanceRuleCondition condition : rule.getConditions()) {
                if ("abnormalType".equals(condition.getKey())) {
                    abnormalTypes.add(Integer.valueOf(condition.getValue()));
                }
            }
            subRule.setAbnormalTypes(abnormalTypes);
            subRules.add(subRule);
        }
    }

    @Override
    public void initDefault() {
        enabled = false;
        subRules = Collections.emptyList();
    }
}
